using System;
using System.Collections.Generic;
using System.Text;

namespace MonoCash.Data
{
    class YahooQuotes : QuoteEngine
    {
        public override int GetQuotes()
        {
            Dictionary<DateTime, List<ObjectModel.Security>> startDateSymbols = new Dictionary<DateTime, List<ObjectModel.Security>>();
            foreach (ObjectModel.Security symbol in GetSecurities())
	        {
        		DateTime LastPrice = ObjectModel.Price.LastPriceDate(symbol);

                if (!startDateSymbols.ContainsKey(LastPrice))
                {
                    startDateSymbols.Add(LastPrice, new List<ObjectModel.Security>());
                }
                startDateSymbols[LastPrice].Add(symbol);
            
	        }
            int counter = 0;
            foreach (DateTime start in startDateSymbols.Keys)
	        {
        		 counter +=  GetQuotes(startDateSymbols[start].ToArray(), start, DateTime.Today);
	        }
            
            return counter;
        }
        public override int RefreshHistory()
        {
            DateTime LastPrice = DateTime.Today;
            DateTime EarliestTransaction = DateTime.Today;
            try
            {
                EarliestTransaction = DateTime.Parse(Program.AppSettings.Data.Transactions.Compute("Min(Date)", "").ToString());
            }
            catch { }

            

            return GetQuotes(GetSecurities(), EarliestTransaction, LastPrice);
        }

        private int GetQuotes(ObjectModel.Security[] symbols, DateTime start, DateTime end)
        {
            int counter = 0;
            foreach (ObjectModel.Security symbol in symbols)
            {
                
                string query = @"http://ichart.finance.yahoo.com/table.csv?s=" + symbol.Symbol;
                query += "&d=" + (end.Month - 1).ToString();
                query += "&e=" + end.Day.ToString();
                query += "&f=" + end.Year.ToString();
                query += "&g=d&a=" + (start.Month - 1).ToString();
                query += "&b=" + start.Day.ToString();
                query += "&c=" + start.Year.ToString();
                query += "&ignore=.csv";

                System.Net.WebRequest request = System.Net.HttpWebRequest.Create(query);
                System.Net.WebResponse response = request.GetResponse();

                System.IO.StreamReader reader = new System.IO.StreamReader(response.GetResponseStream());

                while (!reader.EndOfStream)
                {
                    string line = reader.ReadLine();
                    string[] seps = { "," };
                    string[] data = line.Split(seps, StringSplitOptions.None);
                    try
                    {
                        symbol.Prices.Add(DateTime.Parse(data[0].ToString()), Decimal.Parse(data[6].ToString()));
                        counter++;
                    }
                    catch 
                    {
                        
                    }
                }
                response.Close();
            }
            return counter;
        }
    }
}
